﻿using LogProjectManager;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace ManagementTool.BaseViewModels
{
    public class ModelConvertHelper<T> where T : new()
    {
        public static IList<T> ConvertToModel(DataTable dt)
        {
            IList<T> ts = new List<T>();
            Type type = typeof(T);
            string tempName = "";

            try
            {
                foreach (DataRow dr in dt.Rows)
                {
                    T t = new T();

                    PropertyInfo[] propertys = t.GetType().GetProperties();
                    foreach (PropertyInfo pi in propertys)
                    {
                        tempName = pi.Name;

                        if (dt.Columns.Contains(tempName))
                        {

                            if (!pi.CanWrite) continue;

                            object value = dr[tempName];
                            if (value != DBNull.Value)
                                pi.SetValue(t, value, null);
                        }
                    }
                    ts.Add(t);
                }
            }
            catch (Exception ex)
            {
                LogManager.WriteLog(LogTypeEnum.Exception, "ConvertToModel", ex.Message);
            }
            return ts;
        }
    }
}
